﻿using Kiva7.ViewModels;
using Kiva7.DataRequests;
using Kiva7.DataClasses;

namespace Kiva7
{
    public class MainViewModel : BaseViewModel
    {
        public MainViewModel()
        {
            this._JournalEntries = new JournalEntryViewModel();
            this._Loans = new LoansViewModel(_JournalEntries);
            this._Teams = new TeamViewModel();
            this._Lender = new LenderViewModel(_Loans, _Teams);
            this._Friends = new FriendViewModel();
        }

        private JournalEntryViewModel _JournalEntries;
        /// <summary>
        /// A collection for Journal Entries objects.
        /// </summary>
        public JournalEntryViewModel JournalEntries
        {
            get
            {
                return _JournalEntries;
            }
        }

        private LoansViewModel _Loans;
        /// <summary>
        /// A collection for Loan objects.
        /// </summary>
        public LoansViewModel Loans
        { 
            get
            {
                return _Loans;
            } 
        }

        private FriendViewModel _Friends;
        /// <summary>
        /// A collection for Friend objects.
        /// </summary>
        public FriendViewModel Friends
        {
            get
            {
                return _Friends;
            }
        }


        private TeamViewModel _Teams;
        /// <summary>
        /// A collection for Team objects.
        /// </summary>
        public TeamViewModel Teams
        {
            get
            {
                return _Teams;
            }
        }


        private LenderViewModel _Lender;
        public LenderViewModel Lender
        {
            get
            {
                return _Lender;
            }
            set
            {
                if (value != _Lender)
                {
                    _Lender = value;
                    NotifyPropertyChanged("Lender");
                }
            }
        }

        private bool _HasError;
        public bool HasError
        {
            get
            {
                return _HasError;
            }
            set
            {
                if (_HasError != value)
                {
                    _HasError = value;
                    NotifyPropertyChanged("HasError");
                }
            }
        }

        public void Reload(string lenderId)
        {

            LenderLoad(lenderId);

            Loans.Clear();
            
            LenderLoansLoad(lenderId);

            Teams.Data.Clear();

            LenderTeamsLoad(lenderId);

            Friends.Reload();
        }

        private LenderRequest lenderRequest;
        private void LenderLoad(string lenderId)
        {
            HasError = false;
            if (lenderRequest != null)
            {
                lenderRequest.Cancel();
            }
            else
            {
                lenderRequest = new LenderRequest();
                lenderRequest.OnResponse += new DataRequest<DataResponse.LendersResponse, Lender>.DataRequestDelegate(LenderLoaded);
            }
            if (string.IsNullOrEmpty(lenderId))
            {
                Lender.Set(null);
            }
            else
            {
                Lender.IsLoaded = false;
                lenderRequest.GetLender(lenderId);
            }
        }

        private void LenderLoaded(object sender, LenderRequest.DataRequestEventArgs e)
        {
            Lender.IsLoaded = true;
            if (e.Cancelled 
                || e.Exception != null
                || e.Response == null
                || e.Response.Lenders == null
                || e.Response.Lenders.LenderItems == null
                || e.Response.Lenders.LenderItems.Count == 0)
            {
                Lender.Set(null);
                HasError = true;
            }
            else
            {
                Lender.Set(e.Response.Lenders.LenderItems[0]);
            }
        }

        private LenderLoansRequest lenderLoanRequest;
        private void LenderLoansLoad(string lenderId)
        {

            if (lenderLoanRequest != null)
            {
                lenderLoanRequest.Cancel();
            }
            else
            {
                lenderLoanRequest = new LenderLoansRequest();
                lenderLoanRequest.OnResponse += new DataRequest<DataResponse.LoansResponse, Loan>.DataRequestDelegate(LenderLoansLoaded);
            }
            if (string.IsNullOrEmpty(lenderId))
            {
                Lender = null;
            }
            else
            {
                lenderLoanRequest.GetLoans(lenderId, SortBy.Newest);
            }
        }

        private void LenderLoansLoaded(object sender, LenderLoansRequest.DataRequestEventArgs e)
        {
            if (e.Cancelled
                || e.Exception != null
                || e.Response == null
                || e.Response.Loans == null
                || e.Response.Loans.LoanItems == null
                || e.Response.Loans.LoanItems.Count == 0)
            {
                
            }
            else
            {
                Loans.SetLoans(e.Response.Loans.LoanItems);
            }
        }



        private LenderTeamsRequest lenderTeamRequest;
        private void LenderTeamsLoad(string lenderId)
        {

            if (lenderTeamRequest != null)
            {
                lenderTeamRequest.Cancel();
            }
            else
            {
                lenderTeamRequest = new LenderTeamsRequest();
                lenderTeamRequest.OnResponse += new DataRequest<DataResponse.TeamsResponse, Team>.DataRequestDelegate(LenderTeamsLoaded);
            }
            if (string.IsNullOrEmpty(lenderId))
            {
                Lender = null;
            }
            else
            {
                lenderTeamRequest.GetTeams(lenderId);
            }
        }

        private void LenderTeamsLoaded(object sender, LenderTeamsRequest.DataRequestEventArgs e)
        {
            if (e.Cancelled
                || e.Exception != null
                || e.Response == null
                || e.Response.Teams == null
                || e.Response.Teams.TeamItems == null
                || e.Response.Teams.TeamItems.Count == 0)
            {

            }
            else
            {
                foreach (Team team in e.Response.Teams.TeamItems)
                {
                    Teams.Data.Add(team);
                }
                Teams.IsLoaded = true;
                Teams.PagesCount = 1;
                NotifyPropertyChanged("Teams");
            }
        }

    }
}